package com.wang.gallerybackend.manager.websocket.disruptor;

import cn.hutool.json.JSONUtil;
import com.lmax.disruptor.WorkHandler;
import com.wang.gallerybackend.manager.websocket.PictureEditHandler;
import com.wang.gallerybackend.manager.websocket.model.PictureEditMessageTypeEnum;
import com.wang.gallerybackend.manager.websocket.model.PictureEditRequestMessage;
import com.wang.gallerybackend.manager.websocket.model.PictureEditResponseMessage;
import com.wang.gallerybackend.model.entity.User;
import com.wang.gallerybackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

@Slf4j
@Component
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {


	@Resource
	@Lazy
	private PictureEditHandler pictureEditHandler;

	@Resource
	private UserService userService;

	/**
	 * 消费者消费消息
	 * @param event
	 * @throws Exception
	 */
	@Override
	public void onEvent(PictureEditEvent event) throws Exception {
		PictureEditRequestMessage pictureEditRequestMessage = event.getPictureEditRequestMessage();
		WebSocketSession session = event.getSession();
		User user = event.getUser();
		Long pictureId = event.getPictureId();
		// 获取到消息类别
		String type = pictureEditRequestMessage.getType();
		PictureEditMessageTypeEnum pictureEditMessageTypeEnum = PictureEditMessageTypeEnum.valueOf(type);
		// 调用对应的消息处理方法
		switch (pictureEditMessageTypeEnum) {
			case ENTER_EDIT:
				pictureEditHandler.handleEnterEditMessage(pictureEditRequestMessage, session, user, pictureId);
				break;
			case EDIT_ACTION:
				pictureEditHandler.handleEditActionMessage(pictureEditRequestMessage, session, user, pictureId);
				break;
			case EXIT_EDIT:
				pictureEditHandler.handleExitEditMessage(pictureEditRequestMessage, session, user, pictureId);
				break;
			default:
				PictureEditResponseMessage pictureEditResponseMessage = new PictureEditResponseMessage();
				pictureEditResponseMessage.setType(PictureEditMessageTypeEnum.ERROR.getValue());
				pictureEditResponseMessage.setMessage("消息类型错误");
				pictureEditResponseMessage.setUser(userService.getUserVO(user));
				session.sendMessage(new TextMessage(JSONUtil.toJsonStr(pictureEditResponseMessage)));
		}
	}
}
